<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: VoteController.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_VoteController extends Core_Controller_Action_Standard {
	public function indexAction() {
	
	}
	
	public function removeFileAction() {
		$participant_id = $this->_getParam ( 'song_id', $this->_getParam ( 'participant_id', $this->_getParam ( 'video_id' ) ) );
		if (! $participant_id) {
			return;
		}
		$participant = Engine_Api::_ ()->getDbTable ( 'participants', 'vote' )->find ( $participant_id )->current ();
		if (! $participant) {
			return;
		}
		$participant->delete ();
	}
	
	public function finishAction() {
		$challenge_id = $this->_getParam ( 'challenge_id', 0 );
		if (! $challenge_id) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting does not exists.' ) ) ) );
			return;
		}
		
		$voting = Engine_Api::_ ()->getItem ( 'vote_challenge', $challenge_id );
		if (! $voting) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting does not exists.' ) ) ) );
			return;
		}
		
		if (! $voting->authorization ()->isAllowed ( null, 'edit' )) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You don\'t have permissions to finish voting.' ) ) ) );
			return;
		}
		
		if (! $voting->isStarted ()) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting is not started yet.' ) ) ) );
			return;
		}
		
		$this->view->form = $form = new Contest_Form_Delete ();
		$form->setTitle ( 'Finish Voting' );
		$form->setDescription ( 'Are sure you want to finish voting?' );
		$form->getElement ( 'submit' )->setLabel ( 'Finish Voting' );
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$db = Engine_Api::_ ()->getItemTable ( 'user' )->getAdapter ();
		$db->beginTransaction ();
		
		try {
			
			$oldTz = date_default_timezone_get ();
			date_default_timezone_set ( $viewer->timezone );
			$now = time ();
			date_default_timezone_set ( $oldTz );
			
			$voting->endtime = date ( 'Y-m-d H:i:s', $now );
			$voting->save ();
			
			$db->commit ();
		
		} catch ( Exception $e ) {
			
			$db->rollBack ();
			throw $e;
		
		}
		
		$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting finished.' ) ) ) );
		
		return;
	}
	
	public function startAction() {
		$challenge_id = $this->_getParam ( 'challenge_id', 0 );
		if (! $challenge_id) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting does not exists.' ) ) ) );
			return;
		}
		
		$voting = Engine_Api::_ ()->getItem ( 'vote_challenge', $challenge_id );
		if (! $voting) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting does not exists.' ) ) ) );
			return;
		}
		
		if (! $voting->authorization ()->isAllowed ( null, 'edit' )) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You don\'t have permissions to start voting.' ) ) ) );
			return;
		}
		
		if ($voting->isFinished ()) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting already finished.' ) ) ) );
			return;
		}
		
		if ($voting->isStarted ()) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting already started.' ) ) ) );
			return;
		}
		
		$this->view->form = $form = new Vote_Form_Delete ();
		$form->setTitle ( 'Start Voting' );
		$form->setDescription ( 'Are sure you want to start voting?' );
		$form->getElement ( 'submit' )->setLabel ( 'Start Voting' );
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$db = Engine_Api::_ ()->getItemTable ( 'user' )->getAdapter ();
		$db->beginTransaction ();
		
		try {
			
			$oldTz = date_default_timezone_get ();
			date_default_timezone_set ( $viewer->timezone );
			$now = time ();
			date_default_timezone_set ( $oldTz );
			
			$voting->starttime = date ( 'Y-m-d H:i:s', $now );
			$voting->save ();
			
			$db->commit ();
		
		} catch ( Exception $e ) {
			
			$db->rollBack ();
			throw $e;
		
		}
		
		$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting started.' ) ) ) );
		
		return;
	}
	
	public function deleteAction() {
		$id = $this->_getParam ( 'challenge_id' );
		if (! $id) {
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Not enough parameters.' );
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Not enough parameters.' ) ) ) );
			return;
		}
		
		$voting = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $id )->current ();
		if (! $voting->authorization ()->isAllowed ( null, 'delete' ) || ! $voting->getIdentity ()) {
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You are not allowed to delete this voting.' );
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You are not allowed to delete this voting.' ) ) ) );
			return;
		}
		
		$this->view->form = $form = new Vote_Form_Delete ();
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		$voting->delete ();
		
		$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting was deleted.' );
		$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Voting was deleted.' ) ) ) );
	}
	
	public function styleAction() {
		$id = $this->_getParam ( 'challenge_id', 0 );
		$voting = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $id )->current ();
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			Engine_Api::_ ()->core ()->setSubject ( $voting );
		}
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( null, null, 'edit' )->isValid ())
			return;
		if (! $this->_helper->requireAuth ()->setAuthParams ( null, null, 'style' )->isValid ())
			return;
		
		// Make form
		$this->view->form = $form = new Vote_Form_Style ();
		
		// Get current row
		$table = Engine_Api::_ ()->getDbTable ( 'styles', 'core' );
		$select = $table->select ()->where ( 'type = ?', 'vote_challenge' )->where ( 'id = ?', $voting->getIdentity () )->limit ( 1 );
		
		$row = $table->fetchRow ( $select );
		
		// Check post
		if (! $this->getRequest ()->isPost ()) {
			$form->populate ( array ('style' => (null === $row ? '' : $row->style) ) );
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Cool! Process
		$style = $form->getValue ( 'style' );
		
		// Save
		if (null == $row) {
			$row = $table->createRow ();
			$row->type = 'vote_challenge';
			$row->id = $voting->getIdentity ();
		}
		
		$row->style = $style;
		$row->save ();
		
		$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Your changes have been saved.' );
		$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Your changes have been saved.' ) ) ) );
	}
	
	public function disqualifyAction() {
		$id = $this->_getParam ( 'id' );
		if (! $id) {
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Not enough parameters.' );
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Not enough parameters.' ) ) ) );
			return;
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if (! $viewer->getIdentity ()) {
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Login first.' );
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Login first.' ) ) ) );
			return;
		}
		
		$table = Engine_Api::_ ()->getDbTable ( 'participants', 'vote' );
		$part = $table->find ( $id )->current ();
		$voting = $part->getVoting ();
		
		if (! $part->isOwner ( $viewer ) && ! $voting->isOwner ( $viewer )) {
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You did not authorized to disqualify.' );
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You did not authorized to disqualify.' ) ) ) );
			return;
		}
		
		$this->view->form = $form = new Vote_Form_Delete ();
		$form->setTitle ( 'Disqualify Participant' );
		$form->setDescription ( 'Are sure you want to disqualify participant?' );
		$form->getElement ( 'submit' )->setLabel ( 'Disqualify Participant' );
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$part->delete ();
			$voting->participant_count --;
			$voting->save ();
			
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Participant was disqualified.' );
		$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Participant was disqualified.' ) ) ) );
		
		return;
	}
	
	public function paginationAction() {
		$challenge_id = $this->_getParam ( 'challenge_id' );
		if (! $challenge_id) {
			$this->view->status = 0;
			$this->view->error = $this->view->translate ( 'Not enough parameters.' );
			return;
		}
		
		$this->view->subject = $voting = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $challenge_id )->current ();
		if (! $voting->getIdentity ()) {
			$this->view->status = 0;
			$this->view->error = $this->view->translate ( 'Wrong parameters.' );
			return;
		}
		
		$params = array ('page' => $this->_getParam ( 'page', 1 ), 'ipp' => $this->_getParam ( 'ipp', 20 ), 'sort' => $this->_getParam ( 'sort', '' ) );
		
		$this->view->paginator = $paginator = $voting->getParticipants ( $params );
		$this->view->runonce = $runonce = false;
		$this->view->canVote = $canVote = $voting->isStarted () && $voting->authorization ()->isAllowed ( null, 'vote' );
		
		$this->view->html = $this->view->partial ( $voting->type . '/participants.tpl', 'vote', array ('paginator' => $paginator, 'runonce' => $runonce, 'canVote' => $canVote ) );
	}

}